using System.Collections.Generic;
using System.ComponentModel;
using InstantMessenger.Transports;

namespace InstantMessenger
{
    public class Contact : INotifyPropertyChanged
    {
        private readonly List<Contact> friends;
        private ITransport transport;

        public Contact(string name)
        {
            Name = name;
            transport = new OfflineTransport();
            friends = new List<Contact>();
        }

        public string Name { get; private set; }

        public IEnumerable<Contact> Friends { get { return friends; } }

        public void AddFriends(IEnumerable<Contact> contacts)
        {
            friends.AddRange(contacts);
        }

        public bool IsOnline
        {
            get { return transport.IsOnline(this); }
        }

        public bool DeliverMessage(Contact sender, string message)
        {
            return transport.DeliverMessage(sender.Name, Name, message);
        }

        public void ReplaceTransport(ITransport newTransport)
        {
            // TODO: I don't really like this, but can't think of anything nicer
            transport.RemoveContact(this);
            transport = newTransport;
            transport.AddContact(this);

            // TODO: Perhaps it would be better to fire our own event
            // others are listening to this event to update the online status
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Transport"));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public override string ToString()
        {
            return string.Format("{0} ({1})", Name, transport.DisplayName);
        }
    }
}